from urllib import parse
from urllib.request import urlopen, Request
import simplejson

# POST
url = 'http://httpbin.org/post'
data = parse.urlencode({'name': '张延强', 'age': '30'})
ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 ' \
     'Safari/537.36 '
req = Request(url, headers={
    'User-agent': ua
})

with urlopen(req, data=data.encode()) as res:
    print(simplejson.loads(res.read()))

# GET
data1 = parse.urlencode({'q': '中国'})
url1 = 'https://cn.bing.com/search?{}'.format(data1)
print(url1)
req1 = Request(url1, headers={
    'User-agent': ua
})
res1 = urlopen(req1)
with res1:
    with open('bing.html', 'wb+') as f:
        f.write(res1.read())
        f.flush()
